package org.jeecg.modules.userReports.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.userReports.entity.UserReports;
import org.jeecg.modules.userReports.entity.UserReportsField;
import org.jeecg.modules.userReports.entity.UserReportsRow;
import org.jeecg.modules.userReports.mapper.UserReportsFieldMapper;
import org.jeecg.modules.userReports.mapper.UserReportsRowMapper;
import org.jeecg.modules.userReports.mapper.UserReportsMapper;
import org.jeecg.modules.userReports.service.IUserReportsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: user_reports
 * @Author: jeecg-boot
 * @Date: 2023-02-25
 * @Version: V1.0
 */
@Service
public class UserReportsServiceImpl extends ServiceImpl<UserReportsMapper, UserReports> implements IUserReportsService {

    @Autowired
    private UserReportsMapper userReportsMapper;
    @Autowired
    private UserReportsFieldMapper userReportsFieldMapper;
    @Autowired
    private UserReportsRowMapper userReportsRowMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(UserReports userReports, List<UserReportsField> userReportsFieldList, List<UserReportsRow> userReportsRowList) {
        userReportsMapper.insert(userReports);
        if (userReportsFieldList != null && userReportsFieldList.size() > 0) {
            for (UserReportsField entity : userReportsFieldList) {
                entity.setId(null);
                //外键设置
                entity.setReportId(userReports.getId());
                userReportsFieldMapper.insert(entity);
            }
        }
        if (userReportsRowList != null && userReportsRowList.size() > 0) {
            for (UserReportsRow entity : userReportsRowList) {
                entity.setId(null);
                //外键设置
                entity.setReportId(userReports.getId());
                userReportsRowMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(UserReports userReports, List<UserReportsField> userReportsFieldList, List<UserReportsRow> userReportsRowList) {
        userReportsMapper.updateById(userReports);

        //1.先删除子表数据
        userReportsFieldMapper.deleteByMainId(userReports.getId().toString());
        userReportsRowMapper.deleteByMainId(userReports.getId().toString());

        //2.子表数据重新插入
        if (userReportsFieldList != null && userReportsFieldList.size() > 0) {
            for (UserReportsField entity : userReportsFieldList) {
                //外键设置
                entity.setReportId(userReports.getId());
                entity.setId(null);
                userReportsFieldMapper.insert(entity);
            }
        }
        if (userReportsRowList != null && userReportsRowList.size() > 0) {
            for (UserReportsRow entity : userReportsRowList) {
                //外键设置
                entity.setReportId(userReports.getId());
                entity.setId(null);
                userReportsRowMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        userReportsFieldMapper.deleteByMainId(id);
        userReportsRowMapper.deleteByMainId(id);
        userReportsMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            userReportsFieldMapper.deleteByMainId(id.toString());
            userReportsRowMapper.deleteByMainId(id.toString());
            userReportsMapper.deleteById(id);
        }
    }

    @Override
    public List<Map<String, Object>> getTableByCalcDate(String tableName, String calcDate) {
        List<Map<String, Object>> maps = userReportsMapper.getTableByCalcDate(tableName, calcDate);
        if (maps != null && !maps.isEmpty()) {
            return maps;
        }
        return null;
    }

    @Override
    public Page<Map<String, Object>> getTableQueryDate(String tableName,String slotNo, String beginDate,String endDate, int pageSize, int pageNo) {
        Page page = new Page(pageNo,pageSize,false);
        Page<Map<String, Object>> maps = userReportsMapper.getTableQueryDate(page,tableName,slotNo, beginDate,endDate);
        return maps;
    }

    @Override
    public int getTableTotal(String tableName,String slotNo, String beginDate, String endDate) {
        return userReportsMapper.getTableTotal(tableName,slotNo, beginDate, endDate);
    }

    @Override
    public int addTableRow(String tableName, Map<String, Object> map) {
        return userReportsMapper.insertInto(tableName, map);
    }

    @Override
    public int createTable(String tableName, List<String> cloums) {
        return userReportsMapper.createTable(tableName, cloums);
    }

    @Override
    public List<String> getDateList(String tableName, String slotNo, String beginDate, String endDate) {
        return userReportsMapper.getDateList(tableName, slotNo, beginDate, endDate);
    }

}
